package cn.qf.shop.controller;


import cn.qf.shop.pojo.entity.Maintain;

import cn.qf.shop.pojo.qo.MaintainQO;
import cn.qf.shop.pojo.vo.RespResult;

import cn.qf.shop.service.MaintainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.List;

@EnableSwagger2
@RequestMapping("/main")
@Api(tags = "保修管理业务接口")
@RestController
@RestControllerAdvice
public class MaintainController extends  BaseController {
    private Logger logger = LoggerFactory.getLogger(MaintainController.class);
    @Resource
    MaintainService maintainService;

    /**
     * 查询所有人员信息列表，返回给前端页面
     *
     * @return
     */
    @ResponseBody
    @ApiOperation("保修管理数据展示")
    @PostMapping(value = "/showMain", produces = {"application/json;charset=UTF-8"})
    public RespResult showMain() {
        List<Maintain> maintains = maintainService.showMain();
        RespResult respResult = null;
        if (maintains == null) {
            respResult = super.returnRespWithFailed("找不到保修列表");
        }
        respResult = super.returnRespWithSuccess(maintains);
        return respResult;
    }

    /**
     * 增加一条字段
     *
     * @param maintainQO
     * @return
     */
    @ResponseBody
    @ApiOperation("增加一条数据")
    @PostMapping(value = "/regMain", produces = {"application/json;charset=UTF-8"})
    public RespResult regMain(@RequestBody MaintainQO maintainQO) {
        Maintain maintain = new Maintain();
        BeanUtils.copyProperties(maintainQO,maintain);
        RespResult respResult = new RespResult();
        int i = maintainService.addMain(maintainQO);
        if (i == 1) {
            respResult = showMain();
        } else {
            respResult = returnRespWithFailed("添加失败");
        }
        return respResult;
    }

    /**
     * 点击编辑
     *
     * @param maintainQO
     * @return
     */
    @ResponseBody
    @ApiOperation("修改数据")
    @RequestMapping(value = "/alterMain", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult alterMain(@RequestBody MaintainQO maintainQO) {
        Maintain maintain = new Maintain();
        BeanUtils.copyProperties(maintain,maintainQO);
        RespResult respResult = new RespResult();
        int i = maintainService.updateMain(maintainQO);
        if (i == 1) {
            respResult = showMain();
        } else {
            respResult = returnRespWithFailed("修改失败");
        }
        return respResult;
    }

    /**
     * 点击删除一条记录
     *
     * @param
     * @return
     */
    @ResponseBody
    @ApiOperation("删除一行")
    @RequestMapping(value = "/deleteMainId", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult deleteMainId(int maintainId) {

        RespResult respResult = new RespResult();
        int i = maintainService.deleteMain(maintainId);
        if (i == 1) {
            respResult = showMain();
        } else {
            respResult = returnRespWithFailed("删除失败");
        }
        return respResult;
    }

    /**
     * 点击图标进行全部删除
     *
     * @param maintainIds
     * @return
     */
    @ResponseBody
    @ApiOperation("批量删除")
    @RequestMapping(value = "/deleteAllMain", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult deleteAllMain(@RequestParam(value = "maintainIds[]") List<Integer> maintainIds) {
        int i = maintainService.deleteAllMain(maintainIds);
        RespResult respResult = null;
        if (i == -1) {
            respResult = super.returnRespWithFailed("删除失败");
        } else {
            respResult = showMain();
        }
        return respResult;
    }

    @ResponseBody
    @ApiOperation("用户模糊查询")
    @RequestMapping(value = "/selectAllMain", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult selectAllMain(@RequestParam(required = false) String keyword, @RequestParam(required = false) String createTime, @RequestParam(required = false) String updateTime, @RequestParam(defaultValue = "1") int limit, @RequestParam(defaultValue = "10") int offset) {
        RespResult respResult = null;
        List<MaintainQO> maintainQOS = maintainService.MaintainList(keyword, createTime, updateTime, limit, offset);
        if (maintainQOS.size() == 0) {
            respResult = returnRespWithFailed("找不到实体类");
        } else {
            RespResult respResult1 = returnRespWithSuccess(maintainQOS);
            return respResult1;
        }
        return respResult;
    }
}
